package com.chengwang.bookstory.controller;

import com.chengwang.bookstory.mapper.ShopMapper;
import com.chengwang.bookstory.pojo.AllOrder;
import com.chengwang.bookstory.pojo.MyShop;
import com.chengwang.bookstory.pojo.User;
import com.chengwang.bookstory.service.OrderService;
import com.chengwang.bookstory.service.ShopService;
import com.chengwang.bookstory.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private UserService userService;
    //购买
    @RequestMapping("/buyShop")
    public ModelAndView buyShop(HttpServletRequest request, HttpSession session){
        ModelAndView mv=new ModelAndView();
        String[] booksIds = request.getParameterValues("booksId");
        List<String> booksId=new ArrayList<>();
        if(booksIds!=null && booksIds.length>0){
            booksId=Arrays.asList(booksIds);
        }
        System.out.println(booksId);
//        List<String> booksId= Arrays.asList(request.getParameterValues("booksId"));

        User user = (User) session.getAttribute("user");
        if (booksId !=null && booksId.size()>0) {
            orderService.buyShop(booksId,user.getUserAccount());
        }

        List<MyShop> myShops = shopService.queryAllMyShop(user.getUserAccount());
        mv.addObject("myShopList",myShops);
        mv.setViewName("myshop.html");
        return mv;
    }

    //删除总表单行
    @RequestMapping("/delAll")
    public ModelAndView delAll(HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        String orderId = request.getParameter("orderId");
        String userAccount = request.getParameter("userAccount");
        if (userAccount==null || userAccount.isEmpty()){
            mv.setViewName("redirect:/login.html");
            return mv;
        }
        orderService.deteAll(userAccount,orderId);
        User user1=userService.queryUserByUserAccount(userAccount);
        mv.addObject("userUp",user1);
        List<AllOrder> orders=orderService.queryAllOrder(userAccount);
        mv.addObject("orders",orders);
        mv.setViewName("person.html");
        return mv;
    }
    //退货
    @RequestMapping("/UpdateBad")
    public ModelAndView UpdateBad(HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        String orderId = request.getParameter("orderId");
        String userAccount = request.getParameter("userAccount");
        if (userAccount==null || userAccount.isEmpty()){
            mv.setViewName("redirect:/login.html");
            return mv;
        }
        orderService.UpdateBad(userAccount,orderId);
        User user1=userService.queryUserByUserAccount(userAccount);
        mv.addObject("userUp",user1);
        List<AllOrder> orders=orderService.queryAllOrder(userAccount);
        mv.addObject("orders",orders);
        mv.setViewName("person.html");
        return mv;
    }
    //收款
    @RequestMapping("/UpdateOK")
    public ModelAndView UpdateOK(HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        String orderId = request.getParameter("orderId");
        String userAccount = request.getParameter("userAccount");
        if (userAccount==null || userAccount.isEmpty()){
            mv.setViewName("redirect:/login.html");
            return mv;
        }
        orderService.UpdateOK(userAccount,orderId);
        User user1=userService.queryUserByUserAccount(userAccount);
        mv.addObject("userUp",user1);
        List<AllOrder> orders=orderService.queryAllOrder(userAccount);
        mv.addObject("orders",orders);
        mv.setViewName("person.html");
        return mv;
    }
    //管理端查询所有订单
    @RequestMapping("/queryAllOrder")
    @ResponseBody
    public List<AllOrder> queryAllOrder(){
        List<AllOrder> orderList=orderService.mQueryAllOrder();
        return orderList;
    }
    //查询需要发货的订单
    @RequestMapping("/sendOrder")
    @ResponseBody
    public List<AllOrder> sendOrder(){
        List<AllOrder>orderList=orderService.queryOrderByMoney();
        return orderList;
    }
    //修改成发货状态并展示
    @RequestMapping("/queryAllOrderBySend")
    @ResponseBody
    public List<AllOrder> queryAllOrderBySend(){
        List<AllOrder>orderList=orderService.queryAllOrderBySend();
        return orderList;
    }
    //查询退货订单
    @RequestMapping("/queryOrderByNO")
    @ResponseBody
    public List<AllOrder> queryOrderByNO(){
        List<AllOrder>orderList=orderService.queryOrderByNO();
        return orderList;
    }
}
